﻿using System;
using System.Configuration;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using TWS.Cart.Business;
using TWS.Cart.Data;

public partial class Application_Controls_Utils_FileUploader : System.Web.UI.UserControl
{
	string tempPath, permanentPath;

    protected void Page_Load(object sender, EventArgs e)
    {
		tempPath = Server.MapPath("~/temp/" + Session.SessionID) + "/";
		permanentPath = Server.MapPath(ConfigurationManager.AppSettings["FilePath"].ToString());
    }

	List<filesToUpload> _FilesToUpload = new List<filesToUpload>();
	public List<filesToUpload> FilesToUpload 
	{
		get 
		{
			object temp = ViewState["files"];
			if(temp!=null)
				_FilesToUpload = (List<filesToUpload>)temp;
			
			return _FilesToUpload;
		}
		set 
		{
			_FilesToUpload = value;
			ViewState["files"] = _FilesToUpload;
		}
	}

	protected void ButtonAdd_Click(object sender, EventArgs e)
    {
		HttpPostedFile file = FileUpload01.PostedFile;
		
		if (file.ContentLength > 0)
		{
			//Save files in temp folder until they permanently save.
			if (!Directory.Exists(tempPath))
				Directory.CreateDirectory(tempPath);

			file.SaveAs(tempPath + file.FileName);

			List<filesToUpload> t = FilesToUpload;
			t.Add(new filesToUpload { 
				Name=file.FileName,
				Title=TextBoxTitle.Text,
				IsDefault=CheckBoxIsDefault.Checked,
				ContentLength=file.ContentLength, 
				ContentType=file.ContentType, 
				state= filesToUpload.fileState.newFile 
			});
			FilesToUpload = t;
		}

		TextBoxTitle.Text = "";
		CheckBoxIsDefault.Checked = false;

		DrawFiles();
    }

	protected void ButtonSave_Click(object sender, EventArgs e)
	{
	}

	protected void DrawFiles()
	{
		foreach (var f in FilesToUpload)
		{
			TableRow tr = new TableRow();
			TableCell tc = new TableCell();
			TableCell tc0 = new TableCell();
			TableCell tc1 = new TableCell();
			TableCell tc2 = new TableCell();
			
			tc.Text = f.Name;
			Image img = new Image();
			img.ImageUrl = Path.Combine("~/temp/" + Session.SessionID,f.Name);
			img.Height = Unit.Pixel(40);
			tc0.Controls.Add(img);
			tc1.Text = f.Title;
			tc2.Text = f.IsDefault.ToString();

			tr.Cells.Add(tc0);
			tr.Cells.Add(tc);
			tr.Cells.Add(tc1);
			tr.Cells.Add(tc2);
			TableFiles.Rows.Add(tr);
		}
	}

	public bool Save(long productId)
	{
		bool res = false;
		//Check the following: 
		//	-What is new. 
		//	-What was deleted.
		
		if (!Directory.Exists(permanentPath))
			Directory.CreateDirectory(permanentPath);

		foreach (var f in FilesToUpload)
		{
			//First delete the file
			if (File.Exists(permanentPath + f.Name))
				File.Delete(permanentPath + f.Name);

			File.Move(tempPath + f.Name, permanentPath + f.Name);

			CartTranProductsImage ssPImg = new CartTranProductsImage();
			ssPImg.ProductsId = productId;
			ssPImg.Title = f.Title;
			ssPImg.FileName = f.Name;
			ssPImg.IsDefault = f.IsDefault;
			ssPImg.Save();
		}

		return res;
	}

	[Serializable]
	public class filesToUpload
	{
		public enum fileState
		{ 
			newFile,
			deletedFile
		}

		public int Id { get; set; }
		public string Name { get; set; }
		public int ContentLength { get; set; }
		public string ContentType { get; set; }
		public fileState state { get; set; }
		public string Title { get; set; }
		public bool IsDefault { get; set; }
		public int Width { get; set; }
		public int Height { get; set; }

	}
}
